package com.darren.befriend.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.darren.befriend.BaseActivity;
import com.darren.befriend.R;
import com.darren.befriend.models.BubbleData;
import com.darren.befriend.views.CircleImageView;

/**
 * AwesomeAdapter is a Custom class to implement custom row in ListView
 * 
 * @author Adil Soomro
 * 
 */
public class ChatItemAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<BubbleData> mMessages;
	private LayoutInflater mInflater;

	public ChatItemAdapter(Context context, ArrayList<BubbleData> messages) {

		this.mContext = context;
		this.mMessages = messages;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {

		if (mMessages != null) {

			return mMessages.size();
		}

		return 0;
	}

	@Override
	public Object getItem(int position) {
		return mMessages.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		BubbleData messageItem = mMessages.get(position);
		ViewHolder holder;
		if (convertView == null) {

			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.sms_row, parent, false);
			holder.message = (TextView) convertView
					.findViewById(R.id.message_text);
			holder.icon = (CircleImageView) convertView
					.findViewById(R.id.chat_friend_icon);
			holder.sendingIcon = (ImageView) convertView
					.findViewById(R.id.chat_sending_icon);
			holder.sentTime = (TextView) convertView
					.findViewById(R.id.chat_sent_time);
			convertView.setTag(holder);
		} else {

			holder = (ViewHolder) convertView.getTag();
		}

		if (messageItem.type == 2) {

			holder.sendingIcon.setVisibility(View.VISIBLE);
			holder.message.setVisibility(View.GONE);
			holder.icon.setVisibility(View.GONE);
		} else {

			holder.sendingIcon.setVisibility(View.GONE);
			holder.message.setVisibility(View.VISIBLE);
			holder.icon.setVisibility(View.VISIBLE);
			String sentTime = DateFormat.format("dd-MM-yyyy hh:mm",
					messageItem.date).toString();
			holder.sentTime.setText(sentTime);
			if (position == 0) {

				holder.sentTime.setVisibility(View.VISIBLE);
			} else {

				long previousItem = mMessages.get(position - 1).date.getTime();
				long currentItem = messageItem.date.getTime();
				int diffTimeInMin = (int) ((currentItem - previousItem) / (60 * 1000));
				if (diffTimeInMin >= 2) {

					holder.sentTime.setVisibility(View.VISIBLE);
				} else {

					holder.sentTime.setVisibility(View.GONE);
				}
			}

			holder.message.setText(messageItem.message);
			if (messageItem.avatar.equals("") && messageItem.getPhoto() != null) {

				holder.icon.setImageBitmap(messageItem.getPhoto());
			} else {

				((BaseActivity) mContext).getImageLoader().displayImage(
						messageItem.avatar, holder.icon,
						((BaseActivity) mContext).getOptions());
			}

			LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			int size = mContext.getResources().getDimensionPixelSize(
					R.dimen.comm_size_42dp);
			LayoutParams lp1 = new LayoutParams(size, size);
			lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			if (messageItem.type == 0) {

				holder.message.setBackgroundResource(R.drawable.ic_bubble_me);
				holder.message.setTextColor(Color.WHITE);
				holder.message.setGravity(Gravity.CENTER_VERTICAL
						| Gravity.RIGHT);
				lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				lp.addRule(RelativeLayout.LEFT_OF, R.id.chat_friend_icon);
			} else {

				holder.message.setBackgroundResource(R.drawable.ic_bubble_you);
				holder.message.setTextColor(Color.BLACK);
				holder.message.setGravity(Gravity.CENTER_VERTICAL
						| Gravity.LEFT);
				lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				lp.addRule(RelativeLayout.RIGHT_OF, R.id.chat_friend_icon);
			}

			holder.message.setLayoutParams(lp);
			holder.icon.setLayoutParams(lp1);
			int verPadding = mContext.getResources().getDimensionPixelSize(
					R.dimen.chat_item_padding_vertical);
			int horPadding = mContext.getResources().getDimensionPixelSize(
					R.dimen.chat_item_padding_horizontal);
			holder.message.setPadding(verPadding, horPadding, verPadding,
					horPadding);
		}

		return convertView;
	}

	private static class ViewHolder {

		TextView message, sentTime;
		ImageView icon, sendingIcon;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}
